﻿using System;
using Glue;
using Glue.Internals.Verification;
using Xunit;

namespace GlueTests.MapperVerificationTests
{
    public class When_mapping_nested_relation_and_it_throws_exception
    {
                private readonly MapperVerification<DomainPerson, GuiPerson> verification;

        public class DomainPerson
        {
            public String Name { get; set; }
            public DomainAddress Address { get; set; }
        }

        public class DomainAddress
        {
            public String City { get; set; }
        }

        public class GuiPerson
        {
            public String Name { get; set; }
            public GuiAddress Address { get; set; }
        }

        public class GuiAddress
        {
            public String City { get { return ""; } set { throw new Exception("The nested property throws exception");} }
        }

        public When_mapping_nested_relation_and_it_throws_exception()
        {
            var addressMapping = new Mapping<DomainAddress, GuiAddress>();
            addressMapping.AutoRelateEqualNames();
            var mapping = new Mapping<DomainPerson, GuiPerson>();
            mapping.AddConverter(addressMapping);
            mapping.AutoRelateEqualNames();
            verification = new MapperVerification<DomainPerson, GuiPerson>(mapping);
        }

        [Fact]
        public void Should_throw_meaningful_exception()
        {
            Assert.Throws(typeof(GlueException),()=>verification.AssertMapsCorrectlyTowards(new GuiPerson { Address = new GuiAddress() }));
        }
    }
}
